/**
 * 
 */
package com.vladimirjosu.fhc;

import java.util.Comparator;
import java.util.HashMap;
import java.util.NavigableSet;
import java.util.TreeMap;

/**
 * @author Vladimir
 * 
 */
public class BeautifulStrings implements Comparator<Character> {
	private HashMap<Character, Integer> mCounterMap;
	private HashMap<Character, Integer> mBeautyMap;
	private String mInput;

	public BeautifulStrings(String input) {
		mInput = input;
		mCounterMap = new HashMap<>();
		mBeautyMap = new HashMap<>();
	}

	private void getLettersCount(String string) {
		for (int i = 0; i < string.length(); i++) {
			Character c = string.charAt(i);
			if (mCounterMap.containsKey(c)) {
				Integer integer = mCounterMap.get(c);
				mCounterMap.put(c, integer + 1);
			} else {
				mCounterMap.put(c, 1);
			}
		}
	}

	private int getBeauty(String string) {
		int beauty = 0;
		for (int i = 0; i < string.length(); i++) {
			Character c = string.charAt(i);
			beauty += mBeautyMap.get(c);
		}
		return beauty;
	}

	public String solve() {
		getLettersCount(mInput);
		TreeMap<Character, Integer> map = new TreeMap<Character, Integer>(this);
		map.putAll(mCounterMap);
		NavigableSet<Character> keySet = map.descendingKeySet();
		int max = 26;
		for (Character character : keySet) {
			mBeautyMap.put(character, max);
			max--;
		}
		return String.valueOf(getBeauty(mInput));
	}

	@Override
	public int compare(Character o1, Character o2) {
		return mCounterMap.get(o1) <= mCounterMap.get(o2) ? -1 : 1;
	}

}
